<?php

namespace INT\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use INT\AdminBundle\Entity\Filiere;
use INT\AdminBundle\Form\FiliereType;

class FiliereController extends Controller {

    public function indexAction() {
        $filieres = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Filiere')->findAll();
        return $this->render('INTAdminBundle:Filiere:index.html.twig', array('filieres' => $filieres));
    }

    public function addEditAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('INTAdminBundle:Filiere');
        $id = $request->get('id');
        if ($id == '-1') {
            $filiere = new Filiere();
        } else {
            $filiere = $repository->find($id);
        }
        $form = $this->createForm(new FiliereType(), $filiere);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $filiere = $form->getData();
                $em->persist($filiere);
                $em->flush();
                $filieres = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Filiere')->findAll();
                return $this->render('INTAdminBundle:Filiere:list.html.twig', array('filieres' => $filieres));
            }
        }
        return $this->render('INTAdminBundle:Filiere:newEdit.html.twig', array('form' => $form->createView(), 'id' => $id));
    }

    public function supprimerFiliereAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('INTAdminBundle:Filiere');
        $id = $request->get('id');
        $filiere = $repository->find($id);
        $em->remove($filiere);
        $em->flush();
        $filieres = $this->getDoctrine()->getEntityManager()->getRepository('INTAdminBundle:Filiere')->findAll();
        return $this->render('INTAdminBundle:Filiere:list.html.twig', array('filieres' => $filieres));
    }

}
